#include "mytablemodel.h"
#include <KDGanttView>
#include <KDGanttConstraintModel>
#include <KDGanttDateTimeGrid>
#include <KDGanttGlobal>
#include <KDGanttGraphicsView>
#include <KDGanttItemDelegate>
#include <KDGanttStyleOptionGanttItem>
#include <QAbstractItemModel>
#include <QHeaderView>
MyTableModel::MyTableModel(QObject *parent)
    : QAbstractTableModel{parent}
{

}

void MyTableModel::setModelData(const QList<MyModelItem> &datas)
{
    qDebug()<<"setModelData:"<<QDateTime::currentDateTime();
    //重置model数据之前调用beginResetModel，此时会触发modelAboutToBeReset信号
    beginResetModel();
    //重置model中的数据
    modelData=datas;
    //数据设置结束后调用endResetModel，此时会触发modelReset信号
    endResetModel();
    //注意：reset model后，选中的item会失效，我们可以自己写保存和恢复选中项的逻辑
    //如果表的行列数是固定的，只是数据变更了，我们可以用 dataChanged 信号来请求刷新。
    //emit dataChanged(index(0,0),index(RowMax-1,ColMax-1),QVector<int>());
}

QList<MyModelItem> MyTableModel::getModelData() const
{
    qDebug()<<"getModelData:"<<QDateTime::currentDateTime();
    //将内存数据返回
    return modelData;
}

bool MyTableModel::insertModelData(int row, const MyModelItem &datas)
{
    qDebug()<<"insertModelData:"<<QDateTime::currentDateTime();
    //row为0就是开始，为rowcount就在尾巴
    if(row<0||row>rowCount())
        return false;
    //需要将操作放到beginInsertRows和endInsertRows两个函数调用之间
    beginInsertRows(QModelIndex(), row, row);
    //在接口对应行插入空数据
    modelData.insert(row,datas);
    endInsertRows();
    return true;
}

bool MyTableModel::setHorHeaderData(const QList<QString> &headers)
{
    qDebug()<<"setHorHeaderData:"<<QDateTime::currentDateTime();
    horHeaderData=headers;
    emit headerDataChanged(Qt::Horizontal, 0, headers.count()-1);
    return true;
}

QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
 //   qDebug()<<"headerData:"<<section<<QDateTime::currentDateTime();
    if(orientation == Qt::Horizontal){
        //这里我们只设置居中对齐和文本
        if (role == Qt::DisplayRole){
            //这里把横项列表头的文本设计为可以设置的
            if(section>=0 && section<horHeaderData.count())
                return horHeaderData.at(section);
            return QString("Col %1").arg(section + 1);
        }else if(role == Qt::TextAlignmentRole){
            return Qt::AlignCenter;
        }
    }else{
        if (role == Qt::DisplayRole){
            qDebug()<<"headerdata:"<<section;
            return QString("Row %1").arg(section + 1);
        }
        else if(role == Qt::TextAlignmentRole)
            return Qt::AlignCenter;
    }
    return QVariant();
}
bool MyTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
    qDebug()<<"setHeaderData:"<<QDateTime::currentDateTime();
    if (value != headerData(section, orientation, role)) {
        horHeaderData[section] = value.toString();
        emit headerDataChanged(orientation, section, section);
        return true;
    }
    return false;
}

int MyTableModel::rowCount(const QModelIndex &parent) const
{
 //   qDebug()<<"rowCount:"<<QDateTime::currentDateTime();
    if (parent.isValid())
        return 0;
    //返回表格行数
    return modelData.count();
}

int MyTableModel::columnCount(const QModelIndex &parent) const
{
  //  qDebug()<<"columnCount:"<<QDateTime::currentDateTime();
    if (parent.isValid())
        return 0;
    //返回表格列数
    return 5;
}

QVariant MyTableModel::data(const QModelIndex &index, int role) const
{
//    qDebug()<<"data:"<<index<<QDateTime::currentDateTime();
    if (index.isValid() && index.row() < rowCount() && index.column() < columnCount()) {

        const int row = index.row();
        QDate s1,s2;
        if(modelData.at(row).start<=taskGntStartDate)
            s1=taskGntStartDate;
        else
            s1=modelData.at(row).start;

        if(modelData.at(row).end>=taskGntEndDate)
            s2=taskGntStartDate;
        else
            s2=modelData.at(row).end;


        QDateTime s3,s4;
        s3.fromString(s1.toString("yyyy-MM-dd hh:mm:ss"),"yyyy-MM-dd hh:mm:ss");
        s4.fromString(s2.toString("yyyy-MM-dd hh:mm:ss"),"yyyy-MM-dd hh:mm:ss");
        switch (index.column()) {
        case 0:
            return (role == Qt::DisplayRole)
                       ? QVariant::fromValue(modelData.at(row).title)
                       : QVariant();

        case 1:
            return (role == Qt::DisplayRole)
                       ? QVariant::fromValue(( int )KDGantt::TypeTask)
                       : QVariant();
        case 2:
            return (role == KDGantt::StartTimeRole || role == Qt::DisplayRole)
                       ? QVariant::fromValue(s1)
                       : QVariant();
        case 3:
            return (role == KDGantt::EndTimeRole || role == Qt::DisplayRole)
                       ? QVariant::fromValue(s2)
                       : QVariant();
        case 4:
            return (role == Qt::DisplayRole)
                       ? QVariant::fromValue(modelData.at(row).developName)
                       : QVariant();
        }
    }
    return QVariant();

}


bool MyTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    qDebug()<<"setData:"<<QDateTime::currentDateTime();
    //将界面修改的值进行保存
    if (index.isValid() && role == Qt::EditRole) {
        const int row = index.row();
        switch(index.column())
        {
        case 0: modelData[row].taskName = value.toString(); break;
        case 1: modelData[row].start = value.toDate(); break;
        case 2: modelData[row].end = value.toDate(); break;
        }
        //发送信号触发刷新
        emit dataChanged(index, index, QVector<int>() << role);
        return true;
    }
    return false;
}

Qt::ItemFlags MyTableModel::flags(const QModelIndex &index) const
{
 //   qDebug()<<"flags:"<<index<<QDateTime::currentDateTime();
    if (!index.isValid())
        return Qt::NoItemFlags;
    //单元格允许的操作，至少得是ItemIsEnabled的才能进行其他操作
    return Qt::ItemIsEnabled|Qt::ItemIsSelectable;//|Qt::ItemIsEditable;
}

bool MyTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
    qDebug()<<"insertRows:"<<QDateTime::currentDateTime();
    //row为0就是开始，为rowcount就在尾巴
    if(row<0||count<1||row>rowCount())
        return false;
    //需要将操作放到beginInsertRows和endInsertRows两个函数调用之间
    beginInsertRows(parent, row, row + count - 1);
    for(int i=row;i<row+count;i++)
    {
        //在接口对应行插入空数据
        modelData.insert(i,MyModelItem());
    }
    endInsertRows();
    return true;
}

bool MyTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
    qDebug()<<"removeRows:"<<QDateTime::currentDateTime();
    if(row<0||count<1||row+count>rowCount())
        return false;
    //需要将操作放到beginRemoveRows和endRemoveRows两个函数调用之间
    beginRemoveRows(parent, row, row + count - 1);
    for(int i=row+count-1;i>=row;i--)
    {
        //移除该行数据
        modelData.removeAt(i);
    }
    endRemoveRows();
    return true;
}
